import asyncio
from typing import Dict, List, Any
from .registry import tool_registry
from . import cpu_tools, memory_tools, system_tools, boss_tools
from ..cores.config import logger

# 工具分发
async def dispatch_tool_call(
    tool_name: str,
    arguments: Dict[str, Any]
) -> List[Dict[str, Any]]:
    """分发工具调用"""
    if tool_name not in tool_registry:
        logger.error(f"工具 '{tool_name}' 不存在")
        return [{"text": f"错误: 工具 '{tool_name}' 不存在"}]
    try:
        implementation = tool_registry[tool_name]
        result = await implementation(arguments)
        return result
    except Exception as e:
        logger.error(f"工具 '{tool_name}' 执行失败: {e}")
        return [{"text": f"错误: 工具执行失败 - {str(e)}"}]

async def list_available_tools() -> List[Dict[str, Any]]:
    """列出所有可用工具"""
    tools = []
    for name, implementation in tool_registry.items():
        tools.append({
            "name": name,
            "description": f"工具: {name}",
            "implementation": implementation.__name__
        })
    return tools 